from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
from goods.serializers import SkuSerializer, SKUIndexSerializer, SKUCommentSerializer
from goods.utils import PageNum
from drf_haystack.viewsets import HaystackViewSet

from orders.models import OrderGoods
from .models import GoodsCategory,SKU

# Create your views here.

class CatView(APIView):

    def get(self,request,pk):

        cat3 = GoodsCategory.objects.get(id=pk)
        cat2 = cat3.parent
        cat1 = cat2.parent

        return Response ({'cat1':cat1.name,'cat2':cat2.name,'cat3':cat3.name})

class SKUView(ListAPIView):

    serializer_class = SkuSerializer
    filter_backends = [OrderingFilter]
    Ordering_fields = ('create_time','price','sales')
    pagination_class = PageNum

    def get_queryset(self):
        id = self.kwargs['pk']
        sku = SKU.objects.filter(category=id)

        return sku

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer


class SKUCommentView(ListAPIView):

    def get_queryset(self):
        sku_id = self.kwargs['pk']

        return OrderGoods.objects.filter(sku=sku_id,is_commented=True)

    serializer_class = SKUCommentSerializer